# models/admin/admin_user_model.py
from werkzeug.security import generate_password_hash, check_password_hash

from ..exts import db
from . import BaseModel


class AdminUser(BaseModel):
    __tablename__ = 'tb_admin_user'
    username = db.Column(db.String(32), unique=True, comment='管理员名称')
    _password = db.Column(db.String(256), comment='管理员密码')
    is_delete = db.Column(db.Boolean, default=False, comment='是否删除')
    is_super = db.Column(db.Boolean, default=False, comment='超级管理员')
    permission = db.Column(db.Integer, default=1, comment='权限')

    @property
    def password(self):
        raise Exception("ACCESS Not Allowed")

    @password.setter
    def password(self, password_value):
        self._password = generate_password_hash(password_value)

    def check_password(self, password_value):
        return check_password_hash(self._password, password_value)

    def check_permission(self, permission):
        return self.is_super or permission & self.permission == permission